# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Joan Karadimov <joan.karadimov@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=pywin32
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
url='https://github.com/mhammond/pywin32'
pkgdesc='Python for Windows Extensions (mingw-w64)'
license=('spdx:PSF-2.0')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgver=304
pkgrel=3
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "binutils")
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!buildflags')
source=(https://github.com/mhammond/${_realname}/archive/b${pkgver}.zip
        001-compile-and-setup-fixes.patch
        002-com-extensions-fix.patch
        003-win32-extensions-fix.patch
        004-isapi-fix.patch
        005-swig-fix.patch
        006-mapi-headers-fix.patch
        010-workaround-broken-add-dll-dir.patch)
sha256sums=('aab73a6a0a2723e7d28a78b001cee2a3be641b77268eb947266c49dc053bbe05'
            '06b886d7f3997a8aea4ff2c46a133ee4511fa4f13511ef004ee3b87bf81f79f0'
            '56309a1d174d625031c010be83fc3315aa2cd9127f612ff59c0c5bb38d3ab3cf'
            '20f3ce6b981cca176c4cb1f2d27cc717a81fdd1b9a84d430f810dd5faa17435e'
            '1c2b73382763790d71e5529110d61a9848d3656a91a40cc0921b5cd607d7adaf'
            'a364eb52c9e0098516bb08ca5abb687fff4ee1c2e6d4a53dcbe91e60a6820879'
            'a04e4c8ee86606188c55349c4be755c72a9f03830d6f24828aba6bfa251e030d'
            '0168b0ea893cd252201dbde6baad6cfa4b382bc76ba65657606b6c76ce78262f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
    cd "${srcdir}/${_realname}-b${pkgver}"
    apply_patch_with_msg \
    001-compile-and-setup-fixes.patch \
    002-com-extensions-fix.patch \
    003-win32-extensions-fix.patch \
    004-isapi-fix.patch \
    005-swig-fix.patch \
    006-mapi-headers-fix.patch

    # FIXME: https://github.com/msys2-contrib/cpython-mingw/issues/48
    patch -p1 < "${srcdir}"/010-workaround-broken-add-dll-dir.patch

    cd "${srcdir}"
    rm -rf "python-build-${MSYSTEM}" | true
    cp -r "${_realname}-b${pkgver}" "python-build-${MSYSTEM}"
}

build() {
    msg "Python build for ${MSYSTEM}"
    cd "${srcdir}/python-build-${MSYSTEM}"
    ${MINGW_PREFIX}/bin/python setup.py build --skip-verstamp
}

package() {
    cd "${srcdir}/python-build-${MSYSTEM}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
      --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "win32/License.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
